package rjp.mutation;

import java.util.Iterator;
import java.util.Set;

import rjp.map.SantaMap;
import rjp.solution.MapConnection;
import rjp.solution.Solution;

public abstract class Mutation {
	
	final SantaMap map;
	
	public Mutation(SantaMap map)
	{
		this.map=map;
	}

	public abstract Set<MapConnection> getAdditionalConnections(Solution solution);
	public abstract Set<MapConnection> getObsoleteConnections(Solution solution);
	
	public abstract Solution mutate(Solution solution);
	public float pathImprovement(Solution solution)
	{
		float improvement=0;
		Iterator<MapConnection> iterator= this.getObsoleteConnections(solution).iterator();
		while(iterator.hasNext())
		{
			MapConnection mc=iterator.next();
			improvement+=this.map.getDistance(mc.getA(), mc.getB());
		}
		iterator=this.getAdditionalConnections(solution).iterator();
		while(iterator.hasNext())
		{
			MapConnection mc=iterator.next();
			improvement-=this.map.getDistance(mc.getA(), mc.getB());
		}
		return improvement;
	}
	
	public float solutionImprovement(Solution solution)
	{
		float pathImp=this.pathImprovement(solution);
		float oldScore=solution.getScore();
		float newScore=Math.max(solution.getLenghtA()-pathImp, solution.getLenghtB());
		
		return oldScore-newScore;	
	}
	
	public boolean isLegal(Solution solution)
	{
		boolean legal=true;
		Iterator<MapConnection> i=this.getAdditionalConnections(solution).iterator();
		while(i.hasNext())
		{
			if(solution.isConnection(i.next()))
			{
				legal=false;
			}
		}
		return legal;
	}
	
	
}
